package com.cc.screen;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FFmpegSegmenter {
    private String inputFilePath;
    private String outputFilePath;
    private String zmPath;
    private int segmentTime;

    public FFmpegSegmenter(String inputFilePath, String outputFilePath, int segmentTime,String zmPath) {
        this.inputFilePath = inputFilePath;
        this.outputFilePath = outputFilePath;
        this.segmentTime = segmentTime;
        this.zmPath = zmPath;
    }

    public void segment() {
//        String command = String.format(
//                "ffmpeg -i %s -c copy -map 0 -segment_time %d -f segment %s",
//                inputFilePath, segmentTime, outputFilePath
//        );

        String command = String.format(
                         "ffmpeg  -i %s -ss 0 -f segment -segment_time %d -vcodec copy -reset_timestamps 1 -map 0:0 -an %s",
                inputFilePath, segmentTime, outputFilePath
        );


        try {
            ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();

            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }

            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("Segmentation completed successfully.");
            } else {
                System.out.println("Segmentation failed with exit code " + exitCode);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void mergerZiMu() {

        String command = String.format(
                "ffmpeg -i %s -i %s -c copy -c:s mov_text %s",
                inputFilePath, zmPath, outputFilePath
        );

        try {
            ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();

            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }

            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("Segmentation completed successfully.");
            } else {
                System.out.println("Segmentation failed with exit code " + exitCode);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        FFmpegSegmenter segmenter = new FFmpegSegmenter("E:\\video\\20241125_141851_3.mp4", "E:\\video\\output%03d.mp4", 1);
//        segmenter.segment();
//    }
}
